<template>
  <div class="layout-container">
    <el-container>
      <!-- Sidebar -->
      <el-aside :width="isCollapsed ? '64px' : '200px'" class="sidebar">
        <div class="sidebar-header">
          <div v-if="!isCollapsed" class="logo">
            <span>系统管理</span>
          </div>
          <el-button 
            text 
            @click="toggleCollapse" 
            class="collapse-btn"
            :class="{ 'collapsed': isCollapsed }"
          >
            <el-icon>
              <Fold v-if="!isCollapsed" />
              <Expand v-else />
            </el-icon>
          </el-button>
        </div>
        
        <el-menu
          :default-active="activeMenu"
          class="sidebar-menu"
          :collapse="isCollapsed"
          :collapse-transition="false"
          router
        >
          <el-menu-item index="/home">
            <el-icon><House /></el-icon>
            <template #title>首页</template>
          </el-menu-item>
          <el-menu-item index="/user-management">
            <el-icon><User /></el-icon>
            <template #title>用户管理</template>
          </el-menu-item>
          <el-menu-item index="/dictionary-management">
            <el-icon><Document /></el-icon>
            <template #title>字典管理</template>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <!-- Main Content -->
      <el-container>
        <!-- Header -->
        <el-header class="main-header">
          <div class="header-content">
            <div class="breadcrumb">
              <h2>{{ pageTitle }}</h2>
            </div>
            <div class="user-info">
              <el-dropdown @command="handleCommand">
                <span class="el-dropdown-link">
                  <el-icon><User /></el-icon>
                  {{ userInfo.username }}
                  <el-icon class="el-icon--right"><ArrowDown /></el-icon>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
        </el-header>
        
        <!-- Main Content Area -->
        <el-main class="main-content">
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  House, 
  User, 
  Document,
  Fold, 
  Expand, 
  ArrowDown 
} from '@element-plus/icons-vue'

export default {
  name: 'Layout',
  components: {
    House,
    User,
    Document,
    Fold,
    Expand,
    ArrowDown
  },
  setup() {
    const router = useRouter()
    const route = useRoute()
    const isCollapsed = ref(false)
    const userInfo = ref({})
    
    const activeMenu = computed(() => {
      return route.path
    })
    
    const pageTitle = computed(() => {
      const titles = {
        '/home': '首页',
        '/user-management': '用户管理',
        '/dictionary-management': '字典管理'
      }
      return titles[route.path] || '首页'
    })
    
    onMounted(() => {
      const storedUserInfo = localStorage.getItem('userInfo')
      if (storedUserInfo) {
        userInfo.value = JSON.parse(storedUserInfo)
      }
    })
    
    const toggleCollapse = () => {
      isCollapsed.value = !isCollapsed.value
    }
    
    const handleCommand = async (command) => {
      if (command === 'logout') {
        try {
          await ElMessageBox.confirm('确定要退出登录吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          
          localStorage.removeItem('token')
          localStorage.removeItem('userInfo')
          ElMessage.success('退出成功')
          router.push('/login')
        } catch (error) {
          // User cancelled
        }
      }
    }
    
    return {
      isCollapsed,
      userInfo,
      activeMenu,
      pageTitle,
      toggleCollapse,
      handleCommand
    }
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.sidebar {
  background-color: #304156;
  transition: width 0.28s;
}

.sidebar-header {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  background-color: #263445;
}

.logo span {
  color: #fff;
  font-size: 16px;
  font-weight: bold;
}

.collapse-btn {
  color: #fff;
  padding: 8px;
  min-height: auto;
}

.collapse-btn.collapsed {
  margin: 0 auto;
}

.sidebar-menu {
  border: none;
  background-color: #304156;
}

.sidebar-menu .el-menu-item {
  color: #bfcbd9;
}

.sidebar-menu .el-menu-item:hover,
.sidebar-menu .el-menu-item.is-active {
  background-color: #263445;
  color: #409EFF;
}

.main-header {
  background-color: #fff;
  border-bottom: 1px solid #e4e7ed;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
}

.breadcrumb h2 {
  margin: 0;
  color: #303133;
  font-size: 18px;
}

.user-info .el-dropdown-link {
  color: #606266;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 5px;
}

.main-content {
  background-color: #f0f2f5;
  padding: 0;
  overflow-y: auto;
}
</style>